//
//  SNCHttpTransportUrlConfigManager.m
//  ClaimPlatform_RN
//
//  Created by gongjian on 2018/4/4.
//  Copyright © 2018 sncfc. All rights reserved.
//

#import "JJTHttpTransportUrlConfigManager.h"

@implementation JJTHttpTransportUrlConfigManager

#pragma mark - public

+ (instancetype)sharedInstance
{
    static dispatch_once_t once;
    static JJTHttpTransportUrlConfigManager *instance;
    dispatch_once(&once, ^{
        instance = [[self alloc] init];
    });
    return instance;
}

- (void)addUrlConfig:(NSDictionary *)urlConfigDic_
{
    NSParameterAssert(self.environmentName);
    
    if (!urlConfigDic_)
    {
        return;
    }
    
    if (urlConfigDic_[@"Base"])
    {
        [self __addConfigDic:urlConfigDic_[@"Base"]];
    }
    
    if (urlConfigDic_[@"Environment"])
    {
        [self __addConfigDic:urlConfigDic_[@"Environment"]];
    }
}

- (void)__addConfigDic:(NSDictionary *)configDic_ {
    if (!configDic_)
    {
        return;
    }
    
    if (0 == self.urlConfigDic.count) {
        [self.urlConfigDic addEntriesFromDictionary:configDic_];
    }
    
    [configDic_ enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, NSDictionary * _Nonnull obj, BOOL * _Nonnull stop) {
        if ([self existForIdentity:key]) {
            NSMutableDictionary *mDic = [NSMutableDictionary dictionaryWithDictionary:self.urlConfigDic[key]];
            [mDic addEntriesFromDictionary:obj];
            self.urlConfigDic[key] = mDic;
        } else {
            self.urlConfigDic[key] = obj;
        }
    }];
}

- (NSString *)serverUrl:(NSString *)identity
{
    NSParameterAssert(identity);
    
    NSString *urlString = [[self serverHostWithIdentity:identity] stringByAppendingString:[self serverPathWithIdentity:identity]];
    return urlString;
}

- (NSString *)serverHostWithIdentity:(NSString *)identity
{
    NSParameterAssert(identity);
    
    NSString *hostKey = @"SERVER_HOST_URL";
    
    NSString *host = self.urlConfigDic[self.environmentName][identity][hostKey];
    if (host)
    {
        return host;
    }
    
    NSString *hostEnvironmentConfigIdentity = self.urlConfigDic[identity][@"SERVER_HOST_Environment_Config_Identity"];
    if (hostEnvironmentConfigIdentity)
    {
        host = self.urlConfigDic[hostEnvironmentConfigIdentity][self.environmentName][hostKey];
        if (host)
        {
            return host;
        }
    }
    
    host = self.urlConfigDic[identity][hostKey];
    if (host)
    {
        return host;
    }
    
    host = self.urlConfigDic[self.environmentName][hostKey];
    return host;
}

- (NSString *)serverPathWithIdentity:(NSString *)identity
{
    NSParameterAssert(identity);
    
    NSString *pathKey = @"SERVER_PATH_URL";
    
    NSString *path = self.urlConfigDic[self.environmentName][identity][pathKey];
    if (path)
    {
        return path;
    }
    
    path = self.urlConfigDic[identity][pathKey];
    return path;
}

- (BOOL)existForIdentity:(NSString *)identity
{
    NSParameterAssert(identity);
    
    BOOL exist = (self.urlConfigDic[identity] != nil);
    return exist;
}

#pragma mark - getter and setter

- (NSString *)environmentName
{
    if (!_environmentName)
    {
        _environmentName = @"Product";
    }
    
    return _environmentName;
}

- (NSMutableDictionary *)urlConfigDic
{
    if (_urlConfigDic)
    {
        return _urlConfigDic;
    }
    
    _urlConfigDic = [NSMutableDictionary dictionary];
    return _urlConfigDic;
}

@end
